﻿using System;
using System.Net;
using System.Threading;

namespace Roovent.Network
{
	public class WebClientWithTimeoutLite
	{
		public event DownloadStringCompletedEventHandler DownloadStringCompleted;
		
		public int Timeout
		{
			get { return timeout; }
			set { timeout = value;}
		}

		public WebClientWithTimeoutLite()
		{
			wc.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e)
			{
				if (DownloadStringCompleted != null)
				{
					DownloadStringCompleted(sender, e);
				}
			};
		}

		public void DownloadStringAsync(string uri)
		{
			timer = new Timer(delegate(object o) { wc.CancelAsync(); }, null, timeout, 0);
			wc.DownloadStringAsync(new Uri(uri));
		}

		private WebClient wc = new WebClient();
		private Timer timer;
		private int timeout;
	}
}
